package com.woniuxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.woniuxy.entity.Product;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 马宇航
 * @Todo: TODO
 * @DateTime: 25/07/31/星期四 10:46
 * @Component: 成都蜗牛学苑
 **/
@RestController
@RequestMapping("product")
public class ProductController {
    @Autowired
    ProductService productService;
    //获取的yml中配置的端口值
    @Value("${server.port}")
    String port;

    @GetMapping("/{pid}")
    public Product getProduct(@PathVariable("pid") Integer pid) {
        Product product = productService.getProductById(pid);
        product.setName(product.getName() + ":" + port);
        return product;
    }

    @PutMapping("/{pid}/{num}")
    //服务降级 fallback 指向降级方法
//    @SentinelResource(value = "updateProductStock",fallback = "updateDownStock")
    public ResponseEntity updateProduct(@PathVariable("pid") Integer pid,
                                        @PathVariable("num") Integer num){

        productService.updateProduct(pid, num);
        return ResponseEntity.SUCCESS;
    }

    public ResponseEntity updateDownStock( Integer pid,Integer num,Throwable e){
        return new ResponseEntity("服务器正在维护："+e.getMessage());
    }
    @GetMapping("/{start_stock}/{end_stock}/{start_price}/{end_price}")
    public ResponseEntity findByStore(@PathVariable("start_stock") Integer start_stock,
                                      @PathVariable("end_stock") Integer end_stock,
                                      @PathVariable("start_price") Integer start_price,
                                      @PathVariable("end_price") Integer end_price) {
        List<Product> list = productService.findByStore(start_stock, end_stock, start_price, end_price);
        return ResponseEntity.SUCCESS.putKey(ResponseEntity.ResponseStatus.DATA.value(),list);
    }
}
